/*
 * @Author: 谢小康 2170673706@qq.com
 * @Date: 2024-01-28 19:36:55
 * @LastEditors: 谢小康 2170673706@qq.com
 * @LastEditTime: 2024-01-31 12:31:21
 * @FilePath: \My-bolg\server\serve\comments.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const { Op } = require("sequelize");
let sequelize = require('../model/db.js')
const moment = require('moment')
const comment = require('../model/comment.js')
const danmu = require('../model/danmu.js')
class Comment {
    /* {type,content,username,usersiteurl,usercover}  */
    async addcomments(data) {
        let { type, content, username, usersiteurl, usercover, forid } = data;
        let com = await comment.create({ type, content, username, usersiteurl, usercover, forid })
        if (com) {
            return {
                code: 1,
                msg: '评论成功'
            }
        } else {
            return {
                code: 0,
                msg: '评论失败'
            }
        }
    }
    async getcommentlist(data) {
        let { type, forid } = data;
        let list = await comment.findAll({
            where: {
                type,
                forid
            },
            order: [['createdAt', 'ASC']]
        })
        if (list.length > 0) {
            return {
                code: 1,
                msg: '获取成功',
                data: list
            }
        } else {
            return {
                code: 0,
                msg: '暂无数据',
                data: []
            }
        }
    }
    async getnum(data) {
        let { type, forid } = data;
        let num = await comment.count({
            where: {
                type,
                forid
            }
        })
        console.log(num, "--------------------------");
        if (num > 0) {
            return {
                code: 1,
                msg: '获取成功',
                data: num
            }
        } else {
            return {
                code: 0,
                msg: '暂无评论',
                data: num
            }
        }
    }
    async addreplycomments(data) {
        let { type, content, forid, username, usersiteurl, usercover, fatherid, fromid, fromname } = data;
        let com = await comment.create({ type, content, forid, username, usersiteurl, usercover, fatherid, fromid, fromname })
        if (com) {
            return {
                code: 1,
                msg: '评论成功'
            }
        } else {
            return {
                code: 0,
                msg: '评论失败'
            }
        }
    }
    async setstrs(data) {
        let { id, type, flag } = data;
        let strs = await comment.findOne({
            where: {
                id,
                type
            }
        })
        if (strs) {
            if (flag == 1) {
                let res = await comment.increment("commentstrs", {
                    where: {
                        id,
                        type
                    }
                })
                if (res) {
                    return {
                        code: 1,
                        msg: '点赞成功'
                    }
                } else {
                    return {
                        code: 0,
                        msg: '点赞失败'
                    }
                }
            } else {
                let res = await comment.decrement("commentstrs", {
                    where: {
                        id,
                        type
                    }
                })
                if (res) {
                    return {
                        code: 1,
                        msg: '取消点赞成功'
                    }
                } else {
                    return {
                        code: 0,
                        msg: '取消点赞失败'
                    }
                }
            }
        } else {
            return {
                code: 0,
                msg: '评论不存在',
            }
        }
    }
    async getliuyancommentsnum(data) {
        let { type, forid } = data
        let res = await comment.count({
            where: {
                type,
                forid,
                fatherid:""
            }
        })
        return {
            code: 1,
            data: res,
            msg:"暂无评论"
        }
    }
    async getliuyancomments(data) {
        let { type, forid, page, pageSize } = data
        page = (typeof page == "string") ? parseInt(page) : page;
        pageSize = (typeof pageSize == "string") ? parseInt(pageSize) : pageSize;
        console.log( { type, forid, page, pageSize } );
        let offset = (page - 1) * pageSize
        let res = await comment.findAll({
            offset,
            order: [['createdAt', 'ASC']],
            limit: pageSize,
            where: {
                type,
                forid,
                fromname:""
            }
        })
        let idlist = res.map(item => item.dataValues.id)
        res = res.map(item => item.dataValues)
        let ress = await comment.findAll({
            order: [['createdAt', 'ASC']],
            where: {
                fatherid:{
                    [Op.in]: idlist
                }
            }
        })
        ress = ress.map(item => item.dataValues)
        let datalist = [...res,...ress]
        if (datalist.length > 0) {
            return {
                code: 1,
                msg: '获取成功',
                data: datalist
            }
        } else {
            return {
                code: 0,
                msg: '暂无数据',
                data: []
            }
        }
    }
    async setdanmu(data){
        let res = await danmu.create({content: data})
        if(res){
            return {
                code: 1,
                msg: '发送成功'
            }
        }else{
            return{
                code:2,
                msg:'发送失败'
            }
        }
    }
    async getdanmus(){
        let res = await danmu.findAll({
            order: [['createdAt', 'ASC']]
        })
        if(res){
            return {
                code: 1,
                msg: '获取成功',
                data: res
            }
        }else{
            return{
                code:2,
                msg:'获取失败'
            }
        }
    }
}

module.exports = new Comment();